package com.yjzx.util.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;

import java.util.Objects;

/**
 * @author yjzx
 * @date 2024/8/18
 * @description
 */
@Slf4j
public class StopWatchLog {
    public final StopWatch stopWatch;
    private final boolean isPut;

    public StopWatchLog(String executeName) {
        this(new StopWatch(executeName), true);
    }
    public StopWatchLog(StopWatch executeName) {
        this.stopWatch = executeName;
        this.isPut = true;
        isNull();
    }
    public StopWatchLog(String executeName, boolean isPut) {
        this(new StopWatch(executeName), isPut);
    }

    public StopWatchLog(StopWatch stopWatch, boolean isPut) {
        this.stopWatch = stopWatch;
        this.isPut = isPut;
        isNull();
    }

    public StopWatchLog start(String taskName) {
        stopWatch.start(taskName == null ? "" : taskName);
        return this;
    }

    public void stop(String taskName) {
        stop(taskName, null);
    }

    public void stop(String taskName, String str) {
        stop();
        if (this.isPut) {
            log.info("executeName:{}taskName:{}执行完成,耗时{}秒" + (StringUtils.isNotBlank(str) ? "," + str : ""), stopWatch.getId(), stopWatch.getLastTaskName(), stopWatch.getLastTaskInfo().getTimeSeconds());
        }
        stopWatch.start(taskName);
    }
    public void stopRule(String taskName,String message) {
        stop();
        log.info(message);
        stopWatch.start(taskName);
    }
    public void stopRule(String taskName,String message, Object... var2){
        stop();
        log.info(message, var2);
        stopWatch.start(taskName);
    }



    public void endWatch(String str) {
        stop();
        if (this.isPut) {
            log.info("executeName:{}taskName:{}执行完成,耗时{}秒" + (StringUtils.isNotBlank(str) ? "," + str : ""), stopWatch.getId(), stopWatch.getLastTaskName(), stopWatch.getLastTaskInfo().getTimeSeconds());
            log.info("*****************{}执行完成,共记耗时{}秒*****************", stopWatch.getId(), stopWatch.getTotalTimeSeconds());
        }
    }
    public void endRule(String message) {
        stop();
        log.info(message);
    }
    public void endRule(String message, Object... var2){
        stop();
        log.info(message, var2);
    }



    public void errorWatch(String str) {
        stop();
        double s = stopWatch.getLastTaskInfo().getTimeSeconds();
        if (this.isPut) {
            log.error("executeName:{}taskName:{}发生异常，共耗时{}秒" + (StringUtils.isBlank(str) ? "" : ":     " + str), stopWatch.getId(), stopWatch.getLastTaskName(), s);
        }
    }

    public void errorWatch(Exception e, String str) {
        stop();
        if (this.isPut) {
            log.error("executeName:"+stopWatch.getId()+"taskName:"+stopWatch.getLastTaskName()+"发生异常，共耗时"+stopWatch.getLastTaskInfo().getTimeSeconds()+"秒"+(StringUtils.isBlank(str) ? "" : ":     " + str), e);
        }
    }
    public void errorRule(String message) {
        stop();
        log.error(message);
    }
    public void errorRule(String message,Object... var) {
        stop();
        log.error(message,var);
    }
    public void errorRule(String message,Exception e) {
        stop();
        log.error(message,e);
    }

    private void isNull() {
        if (Objects.isNull(stopWatch)) {
            ExceptionUtil.throwException(2,RuntimeException.class,"StopWatch不能为空");
        }
    }

    private void stop() {
        if (stopWatch.isRunning()) {
            stopWatch.stop();
        }

    }

}
